package com.sanjiahantang.youjiarole.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.config.StaticValue;


/**
 * Created by zy on 2017/9/1.
 */

public abstract class BaseMvpFragment<V extends BaseView, T extends BaseMvpPresenter<V>> extends Fragment implements StaticValue {

    protected BaseMvpActivity context;
    protected TextView title;
    protected ImageView back;
    protected TextView ensure;
    protected FrameLayout right;
    public T presenter;
    protected ImmersionBar immersionBar;

    @Override
    public abstract View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (isImmersionBarEnabled())
            initImmersionBar();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        context = (BaseMvpActivity) getActivity();
        presenter = initPresenter();
        presenter.attach((V) this);
        initTitle();
        initView();// 加载view
        initData();// 加载数据 data
    }

    private void initTitle() {
        View back = getView().findViewById(R.id.back);
        if (back != null) {
            back.setVisibility(View.GONE);
        }
    }


    // 实例化presenter
    public abstract T initPresenter();

    protected abstract void initView();

    protected abstract void initData();

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroy() {
        presenter.detach();
        super.onDestroy();
        if (immersionBar != null)
            immersionBar.destroy();
    }

    public void setTitleText(String titleText) {
        TextView tvTitle = (TextView) getView().findViewById(R.id.title);
        if (tvTitle != null) {
            tvTitle.setText(titleText);
        }
    }

    public View showRightButton(int rid) {
        View fl = getView().findViewById(R.id.flIcon1);
        ImageView iv = (ImageView) getView().findViewById(R.id.icon1);
        iv.setImageResource(rid);
        iv.setVisibility(View.VISIBLE);
        return fl;
    }

    /**
     * 是否在Fragment使用沉浸式
     *
     * @return the boolean
     */
    protected boolean isImmersionBarEnabled() {
        return true;
    }

    /**
     * 初始化沉浸式
     */
    protected void initImmersionBar() {
        immersionBar = ImmersionBar.with(this.getActivity());
        immersionBar.keyboardEnable(true).navigationBarWithKitkatEnable(false).transparentBar().fullScreen(false).navigationBarAlpha(1.0f) .statusBarDarkFont(true, 0.2f)
                .init();
    }
}
